package 滑动窗口;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

/**
 * @description:
 * @author: ywk
 * @date: 2020-09-20
 */
public class 最短子数组 {
    /*
    链接：https://www.nowcoder.com/questionTerminal/4406e29ed847446e9745d75f43fad3fe
来源：牛客网

给定长度为N的整数数组，以及M个互不相同的整数，请找出包含这M个整数的最短子数组。

例如：给定数组[4 2 1 3]，包含数字集{2, 3}的最短子数组是[2 1 3]，包含数字集{1, 3}的最短子数组是[1 3]。
     */
    static boolean[] visited;

    public static void main(String[] args) {
        int[] arr = {1, 1, 3, 4, 3, 4, 4, 1};
        int[] match = {1, 1, 4};
        System.out.println(resolve(arr, match, 0, arr.length));
        System.out.println(findShortestSubArray(arr, match));

    }

    private static int resolve(int[] temp, int[] dif, int start, int end) {
        if (end - start < dif.length) {
            return temp.length;
        }
        int[] newArr = new int[end - start];
        System.arraycopy(temp, start, newArr, 0, end - start);
        if (haveSame(newArr, dif)) {
            int s = newArr.length;
            int s1 = resolve(temp, dif, start + 1, end);
            int s2 = resolve(temp, dif, start, end - 1);
            int s3 = s1 > s2 ? s2 : s1;
            return s3 > s ? s : s3;
        }
        return temp.length;
    }

    //2.动态规划
    public static boolean haveSame(int[] arr1, int[] arr2) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : arr2) {
            map.compute(i, (integer, integer2) -> integer2 == null ? 1 : integer2 + 1);
        }
        int count = arr2.length;
        for (int i : arr1) {
            if (!map.containsKey(i)) {
                return false;
            }
            Integer integer = map.compute(i, (integer1, integer2) -> integer2 - 1);
            if (integer == 0) {
                count--;
            }
            if (count == 0) {
                return true;
            }
        }
        return false;
    }


    public static int findShortestSubArray(int[] nums, int[] targets) {
        Map<Integer, Integer> existTarget = new HashMap<>();
        Map<Integer, Integer> targetMap = new HashMap<>();
        for (int target : targets) {
            targetMap.compute(target, (BinaryOperator<Integer>) (k, v) -> v == null ? 1 : v + 1);
        }
        int len = targets.length;
        int left = 0, right = 0, minLen = Integer.MAX_VALUE;
        while (right < nums.length) {
            if (targetMap.containsKey(nums[right])) {
                int target = nums[right];
                existTarget.compute(target, (BinaryOperator<Integer>) (k, v) -> v == null ? 1 : v + 1);
            }
            right++;
            // int[] arr = {1, 1, 3, 4, 3, 4, 1, 1};
            // int[] match = {1, 1, 4};
            while (same(existTarget, targetMap)) {
                minLen = Math.min(minLen, right - left);
                int num = nums[left];
                if (existTarget.containsKey(num)) {
                    existTarget.compute(num, (BinaryOperator<Integer>) (k, v) -> v == 1 ? null : v - 1);
                }
                left++;
            }
        }
        return minLen == Integer.MAX_VALUE ? -1 : minLen;
    }

    public static Boolean same(Map<Integer, Integer> map1, Map<Integer, Integer> map2) {
        for (Map.Entry<Integer, Integer> entry : map2.entrySet()) {
            Integer value = entry.getValue();
            Integer key = entry.getKey();
            if (map1.containsKey(key) && map1.get(key).compareTo(value) >= 0) {
                continue;
            } else {
                return false;
            }
        }
        return true;
    }
}
